package com.stylefeng.guns.modular.netcar.service.impl;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.model.CommonKeyValue;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.modular.netcar.dao.OrderUserRelationMapper;
import com.stylefeng.guns.modular.netcar.model.OrderUserRelation;
import com.stylefeng.guns.modular.netcar.service.IOrderUserRelationService;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**

 * 订单用户关系表 Service 实现 
 * @author 张亚林
 * @Date 2018-12-27 11:25
 */
@Service
public class OrderUserRelationServiceImpl extends ServiceImpl<OrderUserRelationMapper,OrderUserRelation> implements IOrderUserRelationService {

    @Resource
    private OrderUserRelationMapper orderUserRelationMapper;

    /**
     * 删除订单用户关系表
     * @param orderUserRelation
     * @return     
     */
    @Transactional
    public int deleteOrderUserRelation(OrderUserRelation orderUserRelation,String token)throws Exception{
        if(orderUserRelation==null ){
            throw new RuntimeException("参数不全！");
        }
        return this.orderUserRelationMapper.deleteOrderUserRelation(orderUserRelation);
    }
    /**
     * 批量删除订单用户关系表
     * @param ids 主键ID数组
     * @return 
     */
    @Transactional
    public int deleteBatchOrderUserRelation(String[] ids,String token)throws Exception{
        return this.orderUserRelationMapper.deleteBatchOrderUserRelation(ids);
    }
    /**
     * 查询订单用户关系表列表
     * @param orderUserRelation 订单用户关系表对象
     * @return
     */
    public List<OrderUserRelation> selectListOrderUserRelation(OrderUserRelation orderUserRelation,String token)throws Exception{
        return  this.orderUserRelationMapper.selectListOrderUserRelation(orderUserRelation);
    }
    /**
	 * 分页查询订单用户关系表列表
     * @param pager 分页对象
     * @return
     */     
    public  Map pageListOrderUserRelation(Pager pager,String token) throws Exception{
        Page page=new Page(pager.getPageNumber(),pager.getPageSize());
        page.setRecords(orderUserRelationMapper.selectListOrderUserRelation(page,(OrderUserRelation)pager.getObj()));
        List<OrderUserRelation> list = page.getRecords();
        Map mapDate = new HashMap();
        mapDate.put("rows", list);
        mapDate.put("total", page.getTotal());
        mapDate.put("page", page.getCurrent());
        mapDate.put("pageCount", page.getPages());
        return mapDate;
    }
    /**
     * 同一个事务 互斥锁查询
     * @param orderUserRelation 订单用户关系表对象
     * @return 
     */
    public  List<OrderUserRelation> selectListOrderUserRelationForUpdate(OrderUserRelation orderUserRelation)throws Exception{
        return  this.orderUserRelationMapper.selectListOrderUserRelationForUpdate(orderUserRelation);
    }
    /**
     * 通过主键id查询的订单用户关系表列表
     * @param id 主键id
     * @return     
     */
    public  OrderUserRelation findOrderUserRelationById(String id,String token)throws Exception{
        return  this.orderUserRelationMapper.findOrderUserRelationById(id);
    }

    /**
     * 新增订单用户关系表数据
     * @param orderUserRelation  订单用户关系表对象
     * @return
     */
    @Transactional
    public int insertOrderUserRelation(OrderUserRelation orderUserRelation,String token) throws Exception { 
        if(orderUserRelation!=null && (orderUserRelation.getId()==null || orderUserRelation.getId().trim().length()<10)){
            orderUserRelation.setId(IdWorker.get32UUID());
        }  
        return this.orderUserRelationMapper.insertOrderUserRelation(orderUserRelation);
    }
    
    /**
     * 更新订单用户关系表数据
     * @param orderUserRelation 订单用户关系表对象
     * @return     
     */
    @Transactional
    public int updateOrderUserRelation(OrderUserRelation orderUserRelation,String token) throws Exception {     	
        return  this.orderUserRelationMapper.updateOrderUserRelation(orderUserRelation);
    }    
    /**
     * 查询订单用户关系表返回key-Value属性
     * @param orderUserRelation 系统操作对象
     * @return     
     */
    public List<CommonKeyValue> optionsOrderUserRelation(OrderUserRelation orderUserRelation,String token) throws Exception {
        List<OrderUserRelation> listOrderUserRelation=this.orderUserRelationMapper.selectListOrderUserRelation(orderUserRelation);
        List<CommonKeyValue> listOptions=new ArrayList<CommonKeyValue>();
        for(OrderUserRelation item:listOrderUserRelation){
            CommonKeyValue opts=new CommonKeyValue();
            opts.setId(""+item.getId());
            opts.setValue(item.getName());
            listOptions.add(opts);
        }
        return listOptions;
    }
}
